package cn.tedu.boot23.controller;

import cn.tedu.boot23.utils.DBUtils;
import cn.tedu.boot23.vo.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.dc.pr.PRError;

import java.sql.*;
import java.util.ArrayList;

@RestController
public class EmpController {
    @RequestMapping("/insert")
    public String insert(Emp emp) {
        try (
                Connection conn = DBUtils.getConn()
        ) {
            String sql = "insert into myemp1 values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setString(2, emp.getJob());
            ps.setInt(3, emp.getSal());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加成功<a href='index.html'>回到主页</a>";
    }

    @RequestMapping("/select")
    public String select() {
        ArrayList<Emp> list = new ArrayList<>();
        try (
                Connection conn = DBUtils.getConn()
        ) {
            String sql = "select * from myemp1";
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String job = rs.getString(3);
                int sal = rs.getInt(4);
                list.add(new Emp(id, name, job, sal));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html = "<table border=1>";
        html += "<caption>成员列表</caption>";
        html += "<tr><th>id</th><th>姓名</th><th>工作</th><th>工资</th><th>操作</th></tr>";
        for (Emp emp : list) {
            html += "<tr>";
            html += "<td>" + emp.getId() + "</td>";
            html += "<td>" + emp.getName() + "</td>";
            html += "<td>" + emp.getJob() + "</td>";
            html += "<td>" + emp.getSal() + "</td>";
            html += "<td><a href='/delete?id=" + emp.getId() + "'>删除</a></td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }

    @RequestMapping("/update")
    public String update(Emp emp) {
        System.out.println(emp);
        try (
                Connection conn = DBUtils.getConn()
        ) {
            String sql = "update myemp1 set name=?,job=?,sal=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setString(2, emp.getJob());
            ps.setInt(3, emp.getSal());
            ps.setInt(4, emp.getId());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "成功<a href='/select'>回到用户列表</a>";
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        try (
                Connection conn = DBUtils.getConn()
        ) {
            String sql = "delete from myemp1 where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "完成<a href='index.html'>回到主页</a>";
    }
}
